//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.ms.p2p.plugin.payment.fengfu.util;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;

public class SignatureUtil {
    private static String encodingCharset = "UTF-8";

    public SignatureUtil() {
    }

    public static String keyedEncode(String strSrc, String key) {
        return hmacSign(strSrc, key);
    }

    public static String hmacSign(String aValue, String aKey) {
        byte[] k_ipad = new byte[64];
        byte[] k_opad = new byte[64];

        byte[] keyb;
        byte[] value;
        try {
            keyb = aKey.getBytes(encodingCharset);
            value = aValue.getBytes(encodingCharset);
        } catch (UnsupportedEncodingException var9) {
            keyb = aKey.getBytes();
            value = aValue.getBytes();
        }

        Arrays.fill(k_ipad, keyb.length, 64, (byte)54);
        Arrays.fill(k_opad, keyb.length, 64, (byte)92);

        for(int md = 0; md < keyb.length; ++md) {
            k_ipad[md] = (byte)(keyb[md] ^ 54);
            k_opad[md] = (byte)(keyb[md] ^ 92);
        }

        MessageDigest var10 = null;

        try {
            var10 = MessageDigest.getInstance("MD5");
        } catch (NoSuchAlgorithmException var8) {
            return null;
        }

        var10.update(k_ipad);
        var10.update(value);
        byte[] dg = var10.digest();
        var10.reset();
        var10.update(k_opad);
        var10.update(dg, 0, 16);
        dg = var10.digest();
        return toHex(dg);
    }

    public static String toHex(byte[] input) {
        if(input == null) {
            return null;
        } else {
            StringBuffer output = new StringBuffer(input.length * 2);

            for(int i = 0; i < input.length; ++i) {
                int current = input[i] & 255;
                if(current < 16) {
                    output.append("0");
                }

                output.append(Integer.toString(current, 16));
            }

            return output.toString();
        }
    }

    public static String getHmac(String[] args, String key) {
        if(args != null && args.length != 0) {
            StringBuffer str = new StringBuffer();

            for(int i = 0; i < args.length; ++i) {
                str.append(args[i]);
            }

            return hmacSign(str.toString(), key);
        } else {
            return null;
        }
    }

    public static String digest(String aValue) {
        aValue = aValue.trim();

        byte[] value;
        try {
            value = aValue.getBytes(encodingCharset);
        } catch (UnsupportedEncodingException var5) {
            value = aValue.getBytes();
        }

        MessageDigest md = null;

        try {
            md = MessageDigest.getInstance("SHA");
        } catch (NoSuchAlgorithmException var4) {
            var4.printStackTrace();
            return null;
        }

        return toHex(md.digest(value));
    }
}
